package mspdt.ontrade;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveByModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.RotationByModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.render.RenderTexture;
import org.andengine.opengl.util.GLState;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.ease.EaseBackInOut;
import org.andengine.util.modifier.ease.EaseCircularOut;
import org.andengine.util.modifier.ease.EaseQuadInOut;
import org.andengine.util.modifier.ease.EaseQuadOut;
import org.andengine.util.modifier.ease.EaseSineInOut;
import org.andengine.util.modifier.ease.EaseSineOut;
import org.andengine.util.modifier.ease.EaseStrongOut;


import android.app.AlertDialog;
import android.content.Intent;
import android.hardware.SensorManager;
import android.opengl.GLES20;
import android.os.Bundle;
import android.text.style.ScaleXSpan;
import android.util.Log;
import android.widget.Toast;

public class Game4Activity extends BaseGameActivity implements IOnSceneTouchListener{

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 720;
	private Camera mCamera;
	private Scene mainScene;// chooserScene;
	private Sprite back, info, share;
	private Sound click, beer, water, glass;
    private boolean isCh = false;
    float current_scaleY = 1.0f;
    float drink_time = 7f;
    String SHARE_TEXT = "";
    int which_liq = 0;
	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), mCamera);
		engineOptions.getAudioOptions().setNeedsSound(true);
		return engineOptions;
	}


	
	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("pic/");
		BitmapTextureAtlas infoAtlas = new BitmapTextureAtlas(this.getTextureManager(), 39, 39, TextureOptions.DEFAULT);
		ITextureRegion infoRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(infoAtlas, this, "help.png", 0, 0);
		
		info = new Sprite(430, 670, 40, 40, infoRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionUp())
				{
					final AlertDialog.Builder alert = new AlertDialog.Builder(Game4Activity.this);
					alert.setTitle(getResources().getString(R.string.info_title));
					alert.setMessage(getResources().getString(R.string.game4_info));
					//alert.show();
					Game4Activity.this.runOnUiThread(new Runnable() {
					    @Override
					    public void run() {
					    	alert.show();
//					    	Game1Activity.this.showDialog(MY_DIALOG_ID);
					    }
					});
				}
				return true;
			}
		};
		
		infoAtlas.load();
		SoundFactory.setAssetBasePath("mfx/");
		try {
			this.click = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "click.mp3");
			this.beer = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "game4_beer.mp3");
			this.water = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "game4_water.mp3");
			this.glass = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "game4_cheers.mp3");
		} catch (final IOException e) {
			Debug.e(e);
		}
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}
	public void showShare()
	{
		share.registerEntityModifier(new SequenceEntityModifier(new MoveModifier(0.6f, share.getX(), share.getX(), share.getY(), share.getY()+share.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0)));
	}
	public void choose(int index)
	{
		Log.d("TEST", "index="+index);
		String[] all_share_text = {getResources().getString(R.string.game4_share1), getResources().getString(R.string.game4_share2),
	    		getResources().getString(R.string.game4_share2), getResources().getString(R.string.game4_share4),
	    		getResources().getString(R.string.game4_share5), getResources().getString(R.string.game4_share6),
	    		getResources().getString(R.string.game4_share7), getResources().getString(R.string.game4_share8),
	    		getResources().getString(R.string.game4_share9)};
		SHARE_TEXT = all_share_text[index];
		mainScene.detachChildren();
		mainScene.clearTouchAreas();
		which_liq = index;
		if(index==0)
			beer.play();
		else 
			water.play();
		String[] mug_pics = {"game4_glass_beer.png", "game4_glass_cocktail.png", "game4_glass_coffee.png", "game4_glass_juice.png", "game4_glass_shampain.png"
				, "game4_glass_tea.png", "game4_glass_vodka.png", "game4_glass_water.png" ,"game4_glass_wine.png"};
		String[] liq_pics = {"game4_liq_beer.png", "game4_liq_cocktail.png", "game4_liq_coffee.png", "game4_liq_juice.png", "game4_liq_shampain.png",
				"game4_liq_tea.png", "game4_liq_vodka.png", "game4_liq_water.png", "game4_liq_wine.png"};
		
		float[] liq_X = {140, 140, 110, 140, 140, 60, 140, 140, 140};
		float[] liq_Y = {195, 188, 290, 225, 188, 335, 310, 188, 195};
		float[] min_scale = {0.15f, 0.2f, 0.2f, 0.2f, 0.2f, 0.1f, 0.3f, 0.2f, 0.1f};
		
		int[] liq_W_o = {190, 250, 250, 218, 156, 310, 277, 262, 215};
		int[] liq_H_o = {380, 199, 287, 411, 298, 184, 264, 432, 192};

			BitmapTextureAtlas mugTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 768, 1024, TextureOptions.DEFAULT);
			BitmapTextureAtlas liquidTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), liq_W_o[index], liq_H_o[index], TextureOptions.DEFAULT);
			ITextureRegion mugTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mugTextureAtlas, this, mug_pics[index], 0, 0);
			mugTextureAtlas.load();
			ITextureRegion liquidTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(liquidTextureAtlas, this, liq_pics[index], 0, 0);
			
			final Sprite liq = new Sprite(liq_X[index], liq_Y[index], liq_W_o[index], liq_H_o[index], liquidTextureRegion, mEngine.getVertexBufferObjectManager())
			{
				@Override
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

					if(TouchEvent.ACTION_DOWN == pSceneTouchEvent.getAction())
					{
						current_scaleY = this.getScaleY();
						if(current_scaleY>Float.parseFloat(this.getUserData().toString()))
						{
							if(!isCh)
							{
								switch(which_liq)
								{
								case 0: glass.play(); break;
								case 1: glass.play(); break;
								case 8: glass.play(); break;
								case 4: glass.play(); break;
								case 6: glass.play(); break;
									default: break;
								}
								
								isCh = true;
							}
							drink_time = ((current_scaleY*100)*7)/100-(7*Float.parseFloat(this.getUserData().toString()));
							this.registerEntityModifier(new ScaleModifier(drink_time, this.getScaleX(), this.getScaleX(), current_scaleY, Float.parseFloat(this.getUserData().toString())));
						}
							
					}
					else if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						this.clearEntityModifiers();
						current_scaleY = this.getScaleY();
						drink_time = ((current_scaleY*100)*7)/100-(7*Float.parseFloat(this.getUserData().toString()));
						if(current_scaleY<=Float.parseFloat(this.getUserData().toString()))
						{
							this.registerEntityModifier(new ScaleModifier(1f, this.getScaleX(), this.getScaleX(), Float.parseFloat(this.getUserData().toString()), 1.0f));
							current_scaleY = 1;
						}
					}
					return true;
				}
			};
			
			Sprite mug = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, mugTextureRegion, mEngine.getVertexBufferObjectManager())
			{
				@Override
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
						if(pSceneTouchEvent.isActionUp())
						{
							liq.registerEntityModifier(new ScaleModifier(0.3f, this.getScaleX(), this.getScaleX(), liq.getScaleY(), 1.0f));
							current_scaleY = 1;
							isCh = false;
						}
							
					return true;
				}
			};
			liq.setScaleCenterY(liq.getScaleCenterY()+liq.getHeight()/2);
			if(index!=0)
				liq.registerEntityModifier(new ScaleModifier(3f, liq.getScaleX(), liq.getScaleX(), min_scale[index], 1f));
			else
				liq.registerEntityModifier(new ScaleModifier(5f, liq.getScaleX(), liq.getScaleX(), min_scale[index], 1f));
			liq.setUserData(min_scale[index]);
			liquidTextureAtlas.load();
			
			mainScene.attachChild(liq);
			mainScene.attachChild(mug);
			mainScene.attachChild(back);
			mainScene.registerTouchArea(back);
			//mainScene.attachChild(share);
			mainScene.registerTouchArea(share);
			mainScene.attachChild(info);
			mainScene.registerTouchArea(info);
			mainScene.registerTouchArea(liq);
			mainScene.registerTouchArea(mug);
	}
	@Override
	public void onBackPressed()
	{
		back.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
			@Override
			public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
				Game4Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						click.play();
					}
				});
			}
			@Override
			public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
				Game4Activity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mainScene.clearTouchAreas();
						mainScene.detachChildren();
						mainScene.clearUpdateHandlers();
						mEngine.stop();
						Game4Activity.this.finish();
					}
				});
			}
		}, new SequenceEntityModifier(new MoveModifier(0.6f, back.getX(), back.getX(), back.getY(), back.getY()-back.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
	}
	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// TODO Auto-generated method stub
		
		mainScene = new Scene();
		BitmapTextureAtlas bgTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 768, 1024, TextureOptions.DEFAULT);
		ITextureRegion bgTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bgTextureAtlas, this, "game4_back.jpg", 0, 0);
		bgTextureAtlas.load();
		Sprite bgSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, bgTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				//if(devil!=Integer.parseInt(this.getUserData().toString()))
					//this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
				//male.clearEntityModifiers();
				return true;
			}
		};
		SpriteBackground bg = new SpriteBackground(bgSprite);
		
		
		mainScene.setBackground(bg);
		String[] choose_pics = {"game4_choose_beer.png", "game4_choose_cocktail.png", "game4_choose_coffee.png", "game4_choose_juice.png", "game4_choose_shampain.png"
				, "game4_choose_tea.png", "game4_choose_vodka.png", "game4_choose_water.png" ,"game4_choose_wine.png"};
		float[] choose_X = {-10, 150, -45, 190, 330, 245, 130, 20, 320};
		float[] choose_Y = {20, 20, 430, 210, 210, 480, 410, 210, 20};
		int[] choose_W_o = {240, 201, 300, 114, 101, 300, 235, 132, 123};
		int[] choose_H_o = {300, 300, 259, 300, 300, 179, 300, 300, 300};
		
		for(int i = 0;i<9;i++)
		{
			BitmapTextureAtlas chooseTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), choose_W_o[i], choose_H_o[i], TextureOptions.DEFAULT);
			ITextureRegion chooseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(chooseTextureAtlas, this, choose_pics[i], 0, 0);
			chooseTextureAtlas.load();
			Sprite choose = new Sprite(choose_X[i], choose_Y[i], choose_W_o[i], choose_H_o[i], chooseTextureRegion, mEngine.getVertexBufferObjectManager())
			{
				@Override
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP==pSceneTouchEvent.getAction())
					{
						choose(Integer.parseInt(this.getUserData().toString()));
						return true;
					}
					return false;
				}
			};
			choose.setScale(0.5f);
			choose.setUserData(i);
			mainScene.attachChild(choose);
			mainScene.registerTouchArea(choose);
		}
		BitmapTextureAtlas backTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 127, 127, TextureOptions.DEFAULT);
		ITextureRegion backTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backTextureAtlas, this, "back.png", 0, 0);
		backTextureAtlas.load();
		back = new Sprite(20, -10, 80, 80, backTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						this.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
							@Override
							public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
								Game4Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										click.play();
									}
								});
							}
							@Override
							public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
								Game4Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										mainScene.clearTouchAreas();
										mainScene.detachChildren();
										mainScene.clearUpdateHandlers();
										mEngine.stop();
										Game4Activity.this.finish();
									}
								});
							}
						}, new SequenceEntityModifier(new MoveModifier(0.6f, this.getX(), this.getX(), this.getY(), this.getY()-this.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
					}
				return true;
			}
		};
		BitmapTextureAtlas shareTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 127, 127, TextureOptions.DEFAULT);
		ITextureRegion shareTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(shareTextureAtlas, this, "share.png", 0, 0);
		shareTextureAtlas.load();
		share = new Sprite(370, -10, 80, 80, shareTextureRegion, mEngine.getVertexBufferObjectManager())
		{
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if(TouchEvent.ACTION_UP == pSceneTouchEvent.getAction())
					{
						this.registerEntityModifier(new SequenceEntityModifier(new IEntityModifierListener() {
							@Override
							public void onModifierStarted(final IModifier<IEntity> pModifier, final IEntity pItem) {
								Game4Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										click.play();
										//mainScene.getBackground().registerBackgroundModifier(pBackgroundModifier)
									}
								});
							}

							@Override
							public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
								Game4Activity.this.runOnUiThread(new Runnable() {
									@Override
									public void run() {
										showShare();
										//mEngine.stop();
										//Game3Activity.this.finish();
									}
								});
							}
						}, new SequenceEntityModifier(new MoveModifier(0.6f, share.getX(), share.getX(), share.getY(), share.getY()-share.getHeight(), EaseBackInOut.getInstance()), new DelayModifier(0))));
					}
				return true;
			}
		};
		share.setScaleY(0.9f);
		mainScene.attachChild(back);
		mainScene.registerTouchArea(back);
		//mainScene.attachChild(share);
		mainScene.registerTouchArea(share);
		mainScene.attachChild(info);
		mainScene.registerTouchArea(info);
		pOnCreateSceneCallback.onCreateSceneFinished(this.mainScene);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// TODO Auto-generated method stub
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}




	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		return true;
	}
}
